package com.cgt.springboot.controller;

import com.cgt.springboot.annotation.MyLog;
import com.cgt.springboot.pojo.Admin;
import com.cgt.springboot.pojo.query.AdminQuery;
import com.cgt.springboot.service.IAdminService;
import com.cgt.springboot.util.PageResult;
import com.cgt.springboot.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private IAdminService adminService;

	@RequestMapping("/selectByPage")
//	@ResponseBody
	public PageResult<Admin> selectByPage(AdminQuery adminQuery) {
		System.out.println("AdminController.selectByPage");
		PageResult<Admin> pageResult= adminService.selectByPage(adminQuery);
		return pageResult;
	}

	@MyLog(module = "管理员模块")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
//	@ResponseBody
	public Result add(Admin admin) {
		System.out.println("AdminController.add");
		adminService.add(admin);
		return Result.ok("添加成功");
	}

	@MyLog
	@RequestMapping(value = "/login", method = RequestMethod.POST)
//	@ResponseBody
	public Result login(String name, String password, HttpSession session) {
		System.out.println("AdminController.add");
		Admin admin = adminService.login(name, password);
//		if (admin == null) {
//			return Result.error("用户名或密码错误");
//		} else {
//			session.setAttribute("admin", admin);
//			return Result.ok("登录成功");
//		}
		if (admin == null) {
			return Result.error("用户名或密码错误");
		}

		if (admin.getStatus() == 0) {
			return Result.error("该用户已经禁用");
		}

		session.setAttribute("admin", admin);
		return Result.ok("登录成功");
	}

	@RequestMapping("/logout")
//	@ResponseBody
	public Result logout(HttpSession session) {
		System.out.println("AdminController.logout");
		session.removeAttribute("admin");
		return Result.ok("退出成功");
	}

	@RequestMapping("/updateStatus")
	//@ResponseBody
	public Result updateStatus(Integer id, Integer status) {
		System.out.println("AdminController.updateStatus");
		adminService.updateStatus(id, status);
		return Result.ok("更新状态成功");
	}

	@RequestMapping("/selectAll")
//	@ResponseBody
	public List<Admin> selectAll() {
		System.out.println("StudentController.selectAll");

//		List<Admin> list = new ArrayList<>();
		List<Admin> list = adminService.selectAll();

//		Admin admin1 = new Admin();
//		admin1.setId(1);
//		admin1.setName("zhangsan");
//
//		Admin admin2 = new Admin();
//		admin2.setId(2);
//		admin2.setName("list");
//
//		Admin admin3 = new Admin();
//		admin3.setId(3);
//		admin3.setName("wangwu");
//
//		list.add(admin1);
//		list.add(admin2);
//		list.add(admin3);
		return list;
	}
	@RequestMapping("/toList")
	public String toList(){
		return "admin_list";
	}
}
